package goomi.goods;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import db.mysql.mySqlUtils;

public class goodsService {
	public static void main(String args[]) 
	{
		//test
		try{
//			goodsService color = new goodsService();
//			color.getColors();
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public ArrayList getFitGoods(String userId){
		String sql = "select b.* from fit_info a left join goods b on b.id= a.goods_id";
		if(userId != null && !"all".equals(userId)){
			sql += " where a.member_id = '" + userId + "' ";
		}
		System.out.println(sql);
		return mySqlUtils.Query(sql);
	}
	
	public ArrayList getGoods(String id){
		Connection conn = null;
		try{
			conn = mySqlUtils.getConn();
			Statement st = conn.createStatement();
			String sql = "select * from goods ";
			if(id != null && "1".equals(id)){
				sql += " where line_flag = '1' ";
			}else if(id != null && "2".equals(id)){
				sql += " where fire_flag = '1' ";
			}else if(id != null && "3".equals(id)){
				sql += " where cust_flag = '1' ";
			}else if(id != null && "4".equals(id)){
				sql += " where group_flag = '1' ";
			}else if(id != null && "5".equals(id)){
				sql += " where recommand_flag = '1' ";
			}else if(id != null && !"all".equals(id)){
				sql += " where id = '" + id + "' ";
			} 
			System.out.println(sql);
			ResultSet rs = st.executeQuery(sql);
			ArrayList tmp = new ArrayList();
			while(rs!= null && rs.next()){
				HashMap tmpMap = new HashMap();
				//id name description kind_id create_date line_flag fire_flag cust_flag
				
				if(rs.getString("id")!= null)tmpMap.put("id", rs.getString("id"));
				if(rs.getString("id")!= null)tmpMap.put("db_id", rs.getString("id"));
				if(rs.getString("name")!= null)tmpMap.put("name", rs.getString("name"));
				if(rs.getString("description")!= null)tmpMap.put("description", rs.getString("description"));
				if(rs.getString("kind_id")!= null)tmpMap.put("kind_id", rs.getString("kind_id"));
				if(rs.getString("create_date")!= null)tmpMap.put("create_date", rs.getString("create_date"));
				if(rs.getString("line_flag")!= null)tmpMap.put("line_flag", rs.getString("line_flag"));
				if(rs.getString("fire_flag")!= null)tmpMap.put("fire_flag", rs.getString("fire_flag"));
				if(rs.getString("cust_flag")!= null)tmpMap.put("cust_flag", rs.getString("cust_flag"));
				if(rs.getString("group_flag")!= null)tmpMap.put("group_flag", rs.getString("group_flag"));
				if(rs.getString("picture_id")!= null)tmpMap.put("picture_id", rs.getString("picture_id"));
				
				tmp.add(tmpMap);
			}
			rs.close();
			st.close();
			return tmp;
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn != null){
				try{
					conn.close();
				}catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		return null;
	}
	
	public ArrayList getKind(String id){
		Connection conn = null;
		try{
			conn = mySqlUtils.getConn();
			Statement st = conn.createStatement();
			String sql = "select * from kind_info ";
			System.out.println(sql);
			ResultSet rs = st.executeQuery(sql);
			ArrayList tmp = new ArrayList();
			while(rs!= null && rs.next()){
				HashMap tmpMap = new HashMap();
				//id name description kind_id create_date line_flag fire_flag cust_flag
				
				if(rs.getString("id")!= null)tmpMap.put("db_id", rs.getString("id"));
				if(rs.getString("kind")!= null)tmpMap.put("kind", rs.getString("kind"));
				if(rs.getString("remark")!= null)tmpMap.put("remark", rs.getString("remark"));
				
				tmp.add(tmpMap);
			}
			rs.close();
			st.close();
			return tmp;
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn != null){
				try{
					conn.close();
				}catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		return null;
	}
}
